package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.service.MetaSystemUnitService;
import com.b511.swpu.cementbackend.service.SysMenuService;
import com.b511.swpu.cementbackend.service.SysUserService;
import com.b511.swpu.cementbackend.dto.input.SysUserInputDto;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(value = "公共接口", tags = {"公共接口"})
@RestController
@RequestMapping("/public")
public class PublicController {

    private final SysUserService sysUserService;

    private final MetaSystemUnitService metaSystemUnitService;

    @Autowired
    private SysMenuService sysMenuService;

    public PublicController(SysUserService sysUserService, MetaSystemUnitService metaSystemUnitService) {
        this.sysUserService = sysUserService;
        this.metaSystemUnitService = metaSystemUnitService;
    }

    //获取当前登陆人的账号
    @GetMapping("/getUname")
    public R getUname(HttpServletRequest request){
        return sysUserService.getUname(request);
    }

    @PostMapping("/login")
    public R login(@RequestBody @Validated SysUserInputDto sysUserInputDto) {
        return sysUserService.login(sysUserInputDto);
    }

    //loginFromMainSys接口用于主系统跳转到本系统使用，请勿修改
    @PostMapping("/loginFromMainSys")
    public R loginFromMainSys(@RequestBody @Validated SysUserInputDto sysUserInputDto) {
        return sysUserService.loginFromMainSys(sysUserInputDto);
    }

    //login2接口用于主系统跳转到本系统使用，请勿修改
    @GetMapping("/login2/{data}")
    public R login2(@PathVariable String data, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        request.setAttribute("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Credentials", "true");

        return sysUserService.login2(data);
    }

    @GetMapping("/unit")
    public R getSystemUnit() {
        return metaSystemUnitService.getSystemUnit();
    }

    @GetMapping("/menu")
    public R getSystemMenu() {
        return sysMenuService.getSysMenuTreeOutputDto();
    }
}
